package com.roc.web.action.merinfo;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.roc.bean.MerinfoSearchBean;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.service.IMerchantServices;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.ConfigMerinfoUtil;
import com.roc.util.constant.Constant;
import com.roc.util.constant.MerinfoStatus;
import com.roc.util.constant.Role;

@Controller
@Namespace("/areaMerinfo")
@SuppressWarnings("all")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class MerinfoAreaAction extends PaginationBaseAction {
	private Logger logger = LogManager.getLogger(MerinfoAreaAction.class);
	private MerAreaSearchCondtion searchCondtion;
	@Autowired
	private IMerchantServices merinfoService;
	@Autowired
	IPageService pageService;
	
	private Operators op;
	
	
	@Action(value = "merList", 
			results = { @Result(name = SUCCESS, location = "/pages/merinfo/merAreaQuery.jsp"),
			@Result(name = ERROR, location = "/error.jsp")
	})
	public String queryMerAreaList(){
		try {
			op = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			if(Role.SYSTEM.getCode().equals(op.getRoleType()) || ("2".equals(op.getGroupCd()) && "02".equals(op.getRoleType()) && Long.valueOf("40001").equals(op.getSuperiorId()))){ //直营分公司
			}else{
				addActionError("无权限访问");
				return SUCCESS;
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
			addActionError(e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}
	
	@Action(value = "merAreaList", 
			results = { @Result(name = SUCCESS, location = "/pages/merinfo/merAreaQuery.jsp"),
			@Result(name = ERROR, location = "/error.jsp")
	})
	public String queryMerinfoAreaList(){
		Operators op = (Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(Role.SYSTEM.getCode().equals(op.getRoleType()) || ("2".equals(op.getGroupCd()) && "02".equals(op.getRoleType()) && Long.valueOf("40001").equals(op.getSuperiorId()))){ //直营分公司
				try {
					if(searchCondtion == null){
						searchCondtion = (MerAreaSearchCondtion)getSearchConditionFromSession();
						if(searchCondtion == null){
							searchCondtion = new MerAreaSearchCondtion();
						}
					}
					searchCondtion.setProvNo(op.getAreaCode());
					searchCondtion.setRoleType(op.getRoleType());
					handleResultList(op);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					addActionError(e.getMessage());
					return ERROR;
				}
			}else{
				addActionError("无权限访问");
				return SUCCESS;	
			}	
		return SUCCESS;
	}
	
	/**
	 * 32域商户查询
	 * @param op
	 * @throws Exception
	 */
	public void handleResultList(Operators op) throws Exception{
		try {
			int rows = pageService.countSQL(searchCondtion);
			List<MerinfoSearchBean> msList = new ArrayList<MerinfoSearchBean>();
			if(rows > 0){
				searchRsList = pageService.findAllSQL(searchCondtion, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
				if(searchRsList.size() > 0 && searchRsList != null){
					msList = handleMerinfoDetail(searchRsList, op);
				}
			}
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute("COUNT", rows);//统计次数
			getRequest().setAttribute(SEARCH_RS_LIST, msList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondtion);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 处理查询结果
	 * @param objList
	 * @param op
	 * @return
	 * @throws Exception
	 */
	public List<MerinfoSearchBean> handleMerinfoDetail(List<Object> objList, Operators op) throws Exception{
		List<MerinfoSearchBean> mList = new ArrayList<MerinfoSearchBean>();
			try {
					Iterator iterator = objList.iterator();
					while(iterator.hasNext()){
						Object[] obj = (Object[]) iterator.next();
						MerinfoSearchBean msb = new MerinfoSearchBean();
						msb.setMid(Constance.Null2EmptyTrim(obj[0]));
						msb.setMerName(Constance.Null2EmptyTrim(obj[1]));
						msb.setMerBrName(Constance.Null2EmptyTrim(obj[2]));
						msb.setMerKind(Constance.Null2EmptyTrim(obj[3]));
						msb.setPerName(Constance.Null2EmptyTrim(obj[4]));
						msb.setPerPhone(Constance.Null2EmptyTrim(obj[5]));
						msb.setInterContact(Constance.Null2EmptyTrim(obj[6]));
						msb.setCardNo(Constance.Null2EmptyTrim(obj[7]));
						msb.setFlagCode(Constance.Null2EmptyTrim(obj[8]));
						msb.setAddr(Constance.Null2EmptyTrim(obj[9]));
						String status = Constance.Null2EmptyTrim(obj[10]);
						String statusr = "";
						if(status.length()>0){
							statusr = MerinfoStatus.getName(status);
						}
						msb.setFlag(statusr);//状态
						msb.setMcc(Constance.Null2EmptyTrim(obj[11]));
						msb.setAgentId(Constance.Null2EmptyTrim(obj[12]));
						msb.setCompanyName(Constance.Null2EmptyTrim(obj[13]));
						msb.setRsvl(Constance.Null2EmptyTrim(obj[14]));
						msb.setRsvl3(Constance.Null2EmptyTrim(obj[15]));
						msb.setAmount1(Constance.Null2EmptyTrim(obj[16]));
						msb.setAmount2(Constance.Null2EmptyTrim(obj[17]));
						msb.setGroupCd(Constance.Null2EmptyTrim(obj[18]));
						msb.setNationCd(Constance.Null2EmptyTrim(obj[19]));
						String[] productCodeFlag=merinfoService.queryMchtBizDealArray(msb.getMid(), true, false);
						String[] cycleList={"",""};
						if(Constant.STR_NUMBER_ONE.equals(msb.getGroupCd()) && (Arrays.asList(productCodeFlag).contains(ConfigMerinfoUtil.DIRECT_FUNCTION_T1) ||
								Arrays.asList(productCodeFlag).contains(ConfigMerinfoUtil.DIRECT_FUNCTION_T0))){
							cycleList[0]=Constance.Null2EmptyTrim(obj[22]);
							if(null!=Constance.Null2EmptyTrim(obj[23]) && !Constance.Null2EmptyTrim(obj[23]).equals("")){
								String j="";
								if(null!=cycleList[0] && !cycleList[0].equals("")){
									j="+";
								}
								cycleList[1]=j+Constance.Null2EmptyTrim(obj[23]);
							}
						}else{
							cycleList[0]=Constance.Null2EmptyTrim(obj[20]);
							if(null!=Constance.Null2EmptyTrim(obj[21]) && !Constance.Null2EmptyTrim(obj[21]).equals("")){
								String j="";
								if(null!=cycleList[0] && !cycleList[0].equals("")){
									j="+";
								}
								cycleList[1]=j+Constance.Null2EmptyTrim(obj[21]);
							}
						}
						msb.setMerCycle(cycleList);
						mList.add(msb);
					}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new Exception("查询失败",e);
		}
		return mList;
	}
	
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()	throws Exception {
		return searchCondtion;
	}

	@Override
	protected void setSearchConditionToSubClass(BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_MERAREA_INFO, searchCondition);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		return (MerAreaSearchCondtion) getSession().getAttribute(SEARCH_SESSION_MERAREA_INFO);
	}

	@Override
	protected String getInitPageSize() {
		return Constance.PAGE_MAX_SIZE + "";
	}

	public MerAreaSearchCondtion getSearchCondtion() {
		return searchCondtion;
	}

	public void setSearchCondtion(MerAreaSearchCondtion searchCondtion) {
		this.searchCondtion = searchCondtion;
	}

	public Operators getOp() {
		return op;
	}

	public void setOp(Operators op) {
		this.op = op;
	}
}